import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';

export default class EntryAbility extends UIAbility {
  onCreate(want, launchParam) {
    // Create状态为在应用加载过程中，UIAbility实例创建完成时触发，系统会调用onCreate()回调.
    // 可以在该回调中进行应用初始化操作，例如变量定义资源加载等，用于后续的UI界面展示。
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');

    // 获取UIAbility实例的上下文
    let context = this.context;
    hilog.info(0x000, 'RossZhang', '%{public}s', context);

  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    //  此方法在create之后创建
    // 可以在该回调中设置UI界面加载、设置WindowStage的事件订阅。
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    //  启动首页页面 pages/Index
    windowStage.loadContent('pages/index', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  /**
   * UIAbility实例切换至前台时候触发
   */
  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  /**
   * UIAbility实例切换至后台时候触发
   */
  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
